<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class income extends core {
	public $order_mod, $withdraw_mod;
	public function __construct() {
		parent::__construct();
		$this->order_mod = m("order");
		$this->withdraw_mod = m("withdraw");
	}

	//我的收入首页
	public function index(){
		$can_withdraw_money = $freeze_money = $notok_income = $ok_income = 0;
		//已结算收入，只能结算已经支付了、状态正常的订单的订单
		//$sql = "SELECT sum(commission) FROM yd_order_commission WHERE shop_id='{$this->shop_id}' AND status='1'";
		$sql = "SELECT sum(commission) FROM yd_order_commission oc INNER JOIN yd_order o ON oc.order_id=o.id 
			WHERE oc.shop_id='{$this->shop_id}' AND oc.status='1' AND o.status>0";
		$ok_income = format_price($this->db->get_var($sql));
		//未结算收入，只能计算支付了的订单
		$sql = "SELECT sum(commission) FROM yd_order_commission oc INNER JOIN yd_order o ON oc.order_id=o.id 
			WHERE oc.shop_id='{$this->shop_id}' AND oc.status='0' AND o.status>0";
		$notjiesuan_money = format_price($this->db->get_var($sql));
		$notok_income = $notjiesuan_money;

		$sql = "SELECT can_withdraw_money FROM yd_shop WHERE id='{$this->shop_id}'";
		$can_withdraw_money = format_price($this->db->get_var($sql));
		//不可用金额（正在申请提现）
		$sql = "SELECT withdrawing_money FROM yd_shop WHERE id='{$this->shop_id}'";
		$withdrawing_money = format_price($this->db->get_var($sql));
		$freeze_money = $withdrawing_money;
		success(array("can_withdraw_money"=>$can_withdraw_money, 
			"freeze_money"=>$freeze_money, 
			"ok_income"=>$ok_income, 
			"notok_income"=>$notok_income));
	}

	//订单状态为完成时的佣金收入
	public function ok_income(){
		$this->_my_income(1);
	}
 
	//订单状态为没有完成时的佣金收入
	public function notok_income(){
		$this->_my_income(0);
	} 

	//我的收入
	private function _my_income($income_status) {
		$order_where = "";
		if ($income_status==0) $order_where = " AND o.status>0 ";
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 15;			
		$sql ="SELECT o.id, o.order_sn, oc.commission as money 
			FROM yd_order_commission oc INNER JOIN yd_order o ON oc.order_id=o.id 
			WHERE oc.status='{$income_status}' AND oc.shop_id='{$this->shop_id}' {$order_where} 
			ORDER BY o.id DESC LIMIT {$offset}, {$pagesize}";
		$incomes = $this->db->get_results($sql);
		if ($incomes) {
			foreach ($incomes as $k=>$income) {
				$incomes[$k]->money = format_price($income->money);
			}
		}
		success($incomes);
	}

	//不可用金额
	public function freeze(){
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 15;				
		$sql = "SELECT '' as title, withdraw_money as money, add_time, withdraw_memo as memo, status FROM yd_withdraw 
			WHERE member_id='{$this->member_id}' AND shop_id='{$this->shop_id}' AND status IN (0, 1) ORDER BY id DESC LIMIT {$offset}, {$pagesize}";
		$results = $this->db->get_results($sql);
		if ($results) {
			foreach ($results as $g) {
				$g->title = '申请提现';
				$g->status_name = $this->withdraw_mod->status_name($g->status);
				$g->money = format_price($g->money);
				$g->add_time = date("Y-m-d", $g->add_time);
			}
		}
		success($results);
	} 
}
